package com.xt.core.data;

import java.sql.Blob;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

import com.xt.core.BaseConstants;

/**
 * class for the gs_core application
 * 
 * This class represents the file Object in the application 
 * 
 * @author Author: txiao
 * @version Revision: 1.0, Date: 16.10.2009
 * 
 */
@Entity
@org.hibernate.annotations.Entity(
		dynamicInsert=true, //not insert null property 
		dynamicUpdate=true  //not update unchanged property
)
@Table(name = "gs_file")
@GenericGenerator(name = "incrementGenerator", strategy = "increment") 
public class FileObject extends BasicDBObject{

	private static final long serialVersionUID = -5647109172757523160L;

	@Basic(optional=false) //is not optional
	@Column(name="filename", nullable=false) //is not nullable
	private String name;
	
	@Column(name="filetype")
	private String type;
	
	@Column(name="contenttype")
	private String contentType;
	
	@Column(name="upload")
	private Blob file;
	//private byte[] file;

	@Column(name="created")
	private Date created;
	
	@Column(name="bigness")
	private String size;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public Blob getFile() {
		return file;
	}
	public void setFile(Blob file) {
		this.file = file;
	}

	public Date getCreated() {
		return created;
	}
	
	public void setCreated(Date created) {
		this.created = created;
	}
	
	public String getSize() {
		return size;
	}
	public void setSize(String size){
		this.size = size;
	}
	public void setSize(long size){
		
		if(size<1048576){
			float tempValue = new Float(size)/1024;
			String tmpStr = new Float(tempValue).toString();
			int pos = tmpStr.lastIndexOf('.');
			if(pos != -1){
				if( (pos+4)>tmpStr.length())
					pos = tmpStr.length();
				else
					pos = pos +4;
			}else
				pos = tmpStr.length();
			tmpStr = tmpStr.substring(0, pos); 
			this.size = tmpStr + BaseConstants.KB;
		}else{
			float tempValue = new Float(size)/1048576;
			String tmpStr = new Float(tempValue).toString();
			int pos = tmpStr.lastIndexOf('.');
			if(pos != -1){
				if( (pos+4)>tmpStr.length())
					pos = tmpStr.length();
				else
					pos = pos +4;
			}else
				pos = tmpStr.length();
			tmpStr = tmpStr.substring(0, pos);
			this.size = tmpStr + BaseConstants.MB;
		}
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	/*
	public byte[] getFile() {
		return file;
	}
	public void setFile(byte[] file) {
		this.file = file;
	}
	*/
	
	

	@Override
	public String toString(){
		return  super.toString() + " FileObject{name:" + name + "}";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		FileObject other = (FileObject) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
